/**
 * 
 */
package gsm.cable.server.module.user;

import gsm.cable.server.datastore.DataService;
import gsm.cable.server.datastore.metadata.Kinds;

import java.util.Iterator;

import javax.servlet.http.HttpSession;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.repackaged.org.json.JSONException;
import com.google.appengine.repackaged.org.json.JSONObject;

/**
 * @author acer
 * 
 */
public class UsersAction {

	public static final String SIGNUP_INFO = "signup_info";

	public static void updateUser(User user) {

	}

	public static boolean verifyMobileCode(User user) throws JSONException {
		boolean userVerified = false;
		HttpSession session = user.getSession();
		Object raw = session.getAttribute(SIGNUP_INFO);
		if (raw == null) {
			throw new RuntimeException("No deatil found for the user");
		}
		User sessionUser = new User(new JSONObject((String) raw));
		String userVerificationCode = sessionUser.getUserVerificationCode();
		if (user.getUserVerificationCode().equals(userVerificationCode)) {
			userVerified = true;
		}
		return userVerified;
	}

	public static boolean forgetPasswordVerification(String emailId, String code) {
		boolean passwordVerified = false;
		Query query = new Query(Kinds.USERS);
		query.addFilter(Kinds.User.EMAIL_ID, FilterOperator.EQUAL, emailId);
		query.addFilter(Kinds.User.PASSWORD_VERIFICATION_CODE, FilterOperator.EQUAL, code);
		PreparedQuery prepare = DataService.prepare(query);
		int entitiesCount = 0;
		for (Iterator<Entity> iterator = prepare.asIterator(); iterator.hasNext();) {
			entitiesCount++;
			if (entitiesCount != 1) {
				throw new RuntimeException("Only one row was exppected for emailId [ " + emailId + " ]");
			}
		}
		if (entitiesCount == 0) {
			passwordVerified = true;
		}
		return passwordVerified;
	}

	public boolean emailExist(String emailId) {
		boolean exist = false;
		Query query = new Query(Kinds.USERS);
		query.addFilter(Kinds.User.EMAIL_ID, FilterOperator.EQUAL, emailId);
		PreparedQuery prepare = DataService.prepare(query);
		int entityCount = prepare.countEntities(FetchOptions.Builder.withLimit(2));
		if (entityCount == 1) {
			exist = true;
		}
		return exist;
	}

	public static void signUp(User user) {
		HttpSession session = user.getSession();
		JSONObject json = user.toJSON();
		session.setAttribute(SIGNUP_INFO, json.toString());
	}

	public static boolean signIn(JSONObject userDetail) throws JSONException {
		boolean userVerified = false;
		String emailId = userDetail.getString(Kinds.User.EMAIL_ID);
		String password = userDetail.getString(Kinds.User.PASSWORD);

		Query query = new Query(Kinds.USERS);
		query.addFilter(Kinds.User.EMAIL_ID, FilterOperator.EQUAL, emailId);
		query.addFilter(Kinds.User.PASSWORD, FilterOperator.EQUAL, password);
		PreparedQuery prepare = DataService.prepare(query);
		int entityCount = prepare.countEntities(FetchOptions.Builder.withLimit(2));
		if (entityCount == 1) {
			userVerified = true;
		} else if (entityCount > 1) {
			throw new RuntimeException("Multiple user foiund with emailid [ " + emailId + " ]");
		}
		return userVerified;
	}
}
